package com.huatai.common.httputil;

/**
 * @program: ht-system
 * @description:
 * @author: songxiude
 * @create: 2023-04-25 15:38
 **/

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

import com.huatai.common.util.JsonUtil;
import okhttp3.Cookie;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;

public class HttpResponse implements ResponseSpec, Closeable {
	private final Request request;
	private final Response response;
	private final ResponseBody body;

	HttpResponse(final Response response) {
		this.request = response.request();
		this.response = response;
		this.body = ifNullBodyToEmpty(response.body());
	}

	public int code() {
		return this.response.code();
	}

	public String message() {
		return this.response.message();
	}

	public boolean isOk() {
		return this.response.isSuccessful();
	}

	public boolean isRedirect() {
		return this.response.isRedirect();
	}

	public Headers headers() {
		return this.response.headers();
	}

	public List<Cookie> cookies() {
		return Cookie.parseAll(this.request.url(), this.headers());
	}

	public Request rawRequest() {
		return this.request;
	}

	public Response rawResponse() {
		return this.response;
	}

	public ResponseBody rawBody() {
		return this.body;
	}

	public String asString() {
		try {
			return this.body.string();
		} catch (IOException var2) {
			throw Exceptions.unchecked(var2);
		}
	}

	public byte[] asBytes() {
		try {
			return this.body.bytes();
		} catch (IOException var2) {
			throw Exceptions.unchecked(var2);
		}
	}

	public InputStream asStream() {
		return this.body.byteStream();
	}

	public JsonNode asJsonNode() {
		return JsonUtil.readTree(this.asBytes());
	}

	public <T> T asValue(Class<T> valueType) {
		return JsonUtil.readValue(this.asBytes(), valueType);
	}

	public <T> T asValue(TypeReference<T> typeReference) {
		return JsonUtil.readValue(this.asBytes(), typeReference);
	}

	public <T> List<T> asList(Class<T> valueType) {
		return JsonUtil.readList(this.asBytes(), valueType);
	}

	public <K, V> Map<K, V> asMap(Class<?> keyClass, Class<?> valueType) {
		return JsonUtil.readMap(this.asBytes(), keyClass, valueType);
	}

	public <V> Map<String, V> asMap(Class<?> valueType) {
		return this.asMap(String.class, valueType);
	}

	public <T> T asDomValue(Class<T> valueType) {
		return DomMapper.readValue(this.asStream(), valueType);
	}

	public <T> List<T> asDomList(Class<T> valueType) {
		return DomMapper.readList(this.asStream(), valueType);
	}

	public File toFile(File file) {
		this.toFile(file.toPath());
		return file;
	}

	public Path toFile(Path path) {
		try {
			Files.copy(this.asStream(), path, new CopyOption[0]);
			return path;
		} catch (IOException var3) {
			throw Exceptions.unchecked(var3);
		}
	}

	public MediaType contentType() {
		return this.body.contentType();
	}

	public long contentLength() {
		return this.body.contentLength();
	}

	public String toString() {
		return this.response.toString();
	}

	private static ResponseBody ifNullBodyToEmpty(@Nullable ResponseBody body) {
		return body == null ? Util.EMPTY_RESPONSE : body;
	}

	public void close() throws IOException {
		Util.closeQuietly(this.body);
	}
}

